-- (Decrement)

The Decrement operator is used to decrement the numeric value of a variable by a factor of one. There are two ways of assigning this operator, the first being the pre-decrement method (-- variableName). The numeric contents of the variable are decremented, with this decremented value being returned as the result. The second method, post-decrement (variableName --), decrements the numeric value of the variable by one, but returns the original value in the variable before being decremented. In both methods, if the value of the variable is anything other than a Number data type, an attempt is made to convert it to a number.

syntax:

-- variableName

variableName --

EXAMPLE

var variableOne = new String("500");

newValue = -- variableOne;

document.write("The result in newValue is " + newValue);

newValue2 = variableOne --;

document.write("The new result in newValue2 is " + newValue2);

document.write("The end result in variableOne is " + variableOne);

This example shows both the pre and post decremental methods. The value in variableOne is first converted to a Number data type, then pre-decremented to a value of 499, which is held in the variable newValue. The value in variableOne is then post-decremented to return a value of 499, which is held in the variable newValue2. The end result, which is 498, is now held in the variableOne variable.